﻿/// Copyright © 2013 Microsoft Corporation. 
/// This project is released under the Microsoft Public License (MS-PL)
///
namespace Azure.Performance.Storage
{
    using System;
    using System.IO;
    using Microsoft.WindowsAzure.Storage.Blob;

    using CloudStorageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount;

    public class UploadPerformanceTest : IUploadPerformanceTest
    {
        private CloudBlobClient blobClient;

        private CloudPageBlob pageBlob;

        public void Dispose()
        {
            Clean();
        }

        public void Clean()
        {
            if (null != pageBlob)
                pageBlob.DeleteIfExists();
        }

        public string Id
        {
            get
            {
                return "D6117AAD-7DD1-42B8-97D9-0EA0C039DEB6";
            }
        }

        public string Name
        {
            get
            {
                return "Azure Storage SDK 2.0 Simple Page Blob Upload";
            }
        }

        public string Description
        {
            get
            {
                return "Use Storage Client 2.0 library to upload a Page BLOB with the simple UploadFromStream method: pageBlob.UploadFromStream(filestream)";
            }
        }

        [Setting(Name="Threads", Description = "Number of upload threads to use")]
        public int Threads { get; set; }

        [Setting(Name="FileSize", Description = "Size of the test file to upload")]
        public long FileSize { get; set; }

        [Setting(Name = "SSL", Description = "Use an SSL connection with the upload")]
        public bool UseSsl { get; set; }

        public void Initialize(string storageAccount)
        {
            var account =
                CloudStorageAccount.Parse(storageAccount);

            blobClient = account.CreateCloudBlobClient();
            blobClient.ParallelOperationThreadCount = Threads;

            var uploadContainer = blobClient.GetContainerReference("upload");
            uploadContainer.CreateIfNotExists();

            pageBlob = uploadContainer.GetPageBlobReference(Guid.NewGuid().ToString().ToUpper().Replace("-", "") + ".bin");
        }

        public void Upload(string file)
        {
            pageBlob.UploadFromStream(File.OpenRead(file));
        }
    }
}
